package com.tang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.dto.BookDTO;
import com.tang.dto.CategoryDTO;
import com.tang.util.DeepBeanUtils;
import com.tang.vo.Book;
import com.tang.vo.Category;
import com.tang.service.CategoryService;
import com.tang.mapper.CategoryMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author tang
* @description 针对表【category(分类)】的数据库操作Service实现
* @createDate 2023-04-28 14:43:57
*/
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDTO> all() {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        List<Category> categories = categoryMapper.selectList(wrapper);
        return DeepBeanUtils.copyListProperties(categories, CategoryDTO::new);
    }

    @Override
    public Map<String, Object> list(String name,int pageNum, int pageSize) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        wrapper.eq("parent",0).orderByAsc("sort");
        Map<String,Object> map=new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Category> category = categoryMapper.selectList(wrapper);
        PageInfo<Category> pageInfo=new PageInfo<>(category);
        List<CategoryDTO> categoryDTOS = DeepBeanUtils.copyListProperties(category, CategoryDTO::new);
        map.put("data",categoryDTOS);
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        return map;
    }

    @Override
    public List<CategoryDTO> listChildren(String id) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(id)){
            wrapper.eq("parent",id).orderByAsc("sort");
            List<Category> list = categoryMapper.selectList(wrapper);
            return DeepBeanUtils.copyListProperties(list, CategoryDTO::new);
        }
        return null;
    }

    @Override
    public int update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        if (ObjectUtils.isNotEmpty(categoryDTO.getId())&&!categoryDTO.getId().equals("0")){
            return categoryMapper.updateById(category);
        }
        category.setId(null);
        return categoryMapper.insert(category);
    }

    @Override
    public int delete(String id) {
        if (ObjectUtils.isNotEmpty(id)){
            return categoryMapper.deleteById(id);
        }
        return 0;
    }
}




